﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using YaAppoint.DepartmentUsers;
using YaAppoint.Users;

namespace YaAppoint.Web.Controllers
{
    public class DepartmentUserController(IDepartmentUserAppService departmentUserAppService,
                                          ICurrentUserDepartmentAppService currentUserDepartmentAppService) : AbpController
    {
        private readonly IDepartmentUserAppService _departmentUserAppService= departmentUserAppService;
        private readonly ICurrentUserDepartmentAppService _currentUserDepartmentAppService= currentUserDepartmentAppService;

        /// <summary>
        /// 获取部门树组件数据
        /// </summary>
        [HttpGet("/api/department-user/get-tree-data")]
        public async Task<IActionResult> GetTreeData()
        {
            return new JsonResult(await _departmentUserAppService.GetTreeDataAsync());
        }

        /// <summary>
        ///  获取用户当前登录的部门
        ///  
        /// </summary>
        [HttpGet("/api/department-user/get-current-user-department")]
        public IActionResult GetCurrentUserDepartment()
        {
           var department= _currentUserDepartmentAppService.GetCurrentUserDepartment();
           return new JsonResult(department);
        }

        // 获取部门下用户
        [HttpGet("/api/department-user/get-list")]
        public async Task<IActionResult> GetList(Guid id,string? filterName,int page,int pageSize)
        {
            var res = await _departmentUserAppService.GetListAsync(id, filterName,page,pageSize);
            return new JsonResult(res);
        }

        // 获取可添加到特定科室的用户列表
        [HttpGet("/api/department-user/get-users-for-add")]
        public async Task<IActionResult> GetUsersForAdd(Guid depId, string? filterName, int page, int pageSize)
        {
            var res = await _departmentUserAppService.GetUsersForAdd(depId, filterName, page, pageSize);
            return new JsonResult(res);
        }

        /// <summary>
        /// 为特定科室创建用户
        /// </summary>
        [HttpPost("/api/epartment-user/create-user-for-department")]
        public async Task<IActionResult> CreateUserForDepartment([FromBody]UserCreateDto dto, [FromQuery]Guid depId)
        {
            var res = await _departmentUserAppService.CreateUserForDepartmentAsync(dto, depId);
            return new JsonResult(res);
        }

        /// <summary>
        /// 添加当前用户到科室
        /// </summary>
        [HttpPost("/api/epartment-user/add-current-user-to-department")]
        public async Task<IActionResult> AddCurrentUserToDepartment([FromBody] List<Guid> idList, [FromQuery] Guid depId)
        {
            var res = await _departmentUserAppService.AddCurrentUserToDepartmentAsync(idList, depId);
            return new JsonResult(res);
        }

        /// <summary>
        /// 从特定科室移除用户
        /// </summary>
        [HttpPost("/api/department-user/remove-users")]
        public async Task<IActionResult> RemoveUsers([FromBody] List<Guid> idList, [FromQuery] Guid depId)
        { 
            var res = await _departmentUserAppService.RemoveUsersFromDepartmentAsync(idList, depId);
            return new JsonResult(res);
        }

        /// <summary>
        /// 根据用户ID，删除科室用户
        /// </summary>
        [HttpGet("/api/department-user/delete-department-user-by-user-id")]
        public async Task<IActionResult> DeleteDepartmentUserByUserId(Guid id)
        {
            return new JsonResult(await _departmentUserAppService.DeleteAllDeparmentUsersByUserIdAsync(id));
        }

    }
}
 